package com.ym.b2b.commons.util;

public class ClassLoaderUtil {

    public static Object createInstance(String className) throws ClassNotFoundException, InstantiationException,
                                                         IllegalAccessException {
        Class clasz = Class.forName(className);
        return clasz.newInstance();

    }

    public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
        try {
            // 我们要创建的Class对象
            Class clasz = null;

            // 必需的步骤1：如果类已经在系统缓冲之中
            // 我们不必再次装入它

            clasz = findLoadedClass(name);

            if (clasz != null) {
                // System.out.println(name+"  已经在系统缓冲之中...");
                return clasz;
            }

            // 必需的步骤2：如果上面没有成功
            // 我们尝试用默认的ClassLoader装入它
            if (clasz == null) {
                // System.out.println(name+"  defualt loader forName...");
                try {

                    clasz = Class.forName(name);
                } catch (ClassNotFoundException e) {

                }
            }

            if (clasz == null) {
                // System.out.println(name+"  defualt loader findSystemClass...");
                // clasz = findSystemClass(name);

            }

            if (clasz == null) {
                // System.out.println(name+"  defualt loader findClass...");
                // clasz = findClass(name);
            }

            // 必需的步骤3：如有必要，则装入相关的类
            if (resolve && clasz != null) {
                // resolveClass(clasz);
            }

            // 把类返回给调用者
            return clasz;

        } catch (Exception e) {

        }
        return null;
    }

    private Class findLoadedClass(String name) {
        // TODO Auto-generated method stub
        return null;
    }

}
